#include "projectdefinitions.h"
/*  'A' for master start comunication //master command
	'a' for slave start comunication after //slave command
	'B' for player_sum command //master command
	'b' for player_sum command confirmation //slave command
	'!'-'2' for player_sum //master command 33-50 ASCII; 4-21 sum
	'u' for player_sum confirm//slave command
	'C' for dealer_sum command// master command
	'c' for dealer_sum command confirm //slave command
	'3'-':' for dealer_sum// master command 51-60 ASCII; 2-11 sum	
	'v' for dealer_sum confirm; //slave command
	'd' for hit//slave command
	'D' hit confirmation//master command
	'e' for stand// slave command
	'E' for stand confirmation// master command
*/

char prob_table[7][10]={{1,1,1,1,1,1,1,1,1,1},
						{1,1,0,0,0,1,1,1,1,1},
						{0,0,0,0,0,1,1,1,1,1},
						{0,0,0,0,0,1,1,1,1,1},
						{0,0,0,0,0,1,1,1,1,1},
						{0,0,0,0,0,1,1,1,1,1},
						{0,0,0,0,0,0,0,0,0,0}};
	//What to do after checking 2 cards against 1
	//the table is able in a .jpeg file
	//the fist row is for 11 o lesser
	//the last one is for 17 o greater
	//1 for hit, 0 for stand

PROGRAM_INIT
INIT_UART
ACTIVATE_LCD
CLEAR_LCD

char receive;
int player,dealer,refresh=0;
char decision,state=0;//0 for ill, 1 for online, 2 for player update, 3 for player confirm
					  //4 for dealer update, 5 for dealer confirm,
LCD_TEXT(" Offline...")
PROGRAM_START

UART_READ_CHAR(receive)
if(receive==0)
{
	if(state==0) refresh=refresh+1;
	if (refresh>32000)
	{
		CLEAR_LCD
		LCD_XY(2,1)
		LCD_TEXT("Offline...")
		refresh=0;
	}
}
if(receive=='A')
{
	if(state==0)
	{	
		//LCD_CHAR('a')
		CLEAR_LCD
		LCD_TEXT(" Online     ")
		UART_CHAR('a')
		state=1;
	}
}
if(receive=='B')
{
	if(state==1)
	{
		//LCD_CHAR('b')
		UART_CHAR('b')
		state=2;
	}
}
if((receive>32)&&(receive<51))
{
	if(state==2)
	{
		player=receive-29;
		//LCD_NUMBER(player)
		//LCD_CHAR('$')
		//LCD_CHAR(receive)
		//LCD_CHAR(' ')
		state=3;
	}
	UART_CHAR('u')
}
if(receive=='C')
{
	if(state==3)
	{
		UART_CHAR('c')
		state=4;
	}
}
if((receive>50)&&(receive<61))
{
	if(state==4)
	{
		dealer=receive-49;
		LCD_XY(13,2)
		LCD_TEXT("P ")
		LCD_NUMBER(player)
		LCD_XY(13,3)
		LCD_TEXT("D ")
		LCD_NUMBER(dealer)
		if(player<=11)
			player=0;
		if((player>11)&(player<17))
			player=player-11;
		if(player>16)
			player=6;
		dealer=dealer-2;
		//LCD_CHAR('@')
		//LCD_NUMBER(player)
		//LCD_CHAR('@')
		//LCD_NUMBER(dealer)
		//LCD_CHAR('@')
		decision=prob_table[player][dealer];
		state=5;
		//LCD_CHAR('#')
		//LCD_NUMBER(decition)
		//LCD_CHAR('#')
	}
	UART_CHAR('v')
}
if(receive=='D')
{
	if(state==5)
	{
		if(decision==1)
		{		
			UART_CHAR('d')
			//LCD_CHAR('d')
		}
		if(decision==0)
		{
			UART_CHAR('e')
			//LCD_CHAR('e')
		}
	state=6;
	}
}
if(receive=='E')
{
	if(state==6)
	{
		LCD_XY(11,4)
		if(decision==0)	LCD_TEXT("STAND")
		if(decision==1)	LCD_TEXT("HIT")
		UART_CHAR(0)
		state=0;
	}
}

PROGRAM_END
